
const { save, find, del ,update} = require('../model/studentsModel')

//添加
const addStu = async (req, res, next) => {
    const data = req.body;
    const result = await save(data);
    if (result) {
        res.send({ status: 200, state: true, msg: '增加成功', data: result })
    } else {
        res.send({ status: 400, state: false, msg: '增加信息出错' })
    }
}
//查询
const getStuList = async (req, res) => {
    const result = await find();
    if (result.length) {
        res.send({ status: 200, state: true, total: result.length, data: result })
    } else {
        res.send({ status: 400, state: false, msg: '查询出错' })
    }
}
//删除
const delStu = async (req, res) => {
    const { _id } = req.query;
    const query = { _id };
    const result = await del(query)
    if (result.n) {
        res.send({ status: 200, state: true, msg: '删除成功' })
    } else {
        res.send({ status: 400, state: false, msg: '删除出错' })
    }
}
//修改
const updateStu=async(req,res)=>{
    const {_id}=req.body;
    const query={_id};
    const updated=req.body;
    const result=await update(query,updated)
    if(result.nModified!==0){
        res.send({ status: 200, state: true, msg: '修改成功' })
    }else{
        res.send({ status: 400, state: false, msg: '修改出错' })
    }

}
module.exports = {
    addStu,
    getStuList,
    delStu,
    updateStu
}